package com.example.kuayu;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by 95577 on 2018/6/30.
 */
public class CrosFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        System.out.println("-----------------------------------CrosFilter.doFilter");

        HttpServletResponse res = (HttpServletResponse) servletResponse;
        // 允许所有域名(带有cookie的场合不行)
        // res.addHeader("Access-Control-Allow-Origin", "*");
        res.addHeader("Access-Control-Allow-Origin", "http://localhost:8000");
        // 允许所有方法
        res.addHeader("Access-Control-Allow-Methods", "*");
        // res.addHeader("Access-Control-Allow-Methods", "get");
        res.setHeader("Access-Control-Allow-Headers", "Content-Type,Access-Token,x-header1,x-header2");
        // 预检命令缓存时间
        res.setHeader("Access-Control-Max-Age", "3600");
        // 允许cookie
        res.setHeader("Access-Control-Allow-Credentials", "true");


        filterChain.doFilter(servletRequest, res);
    }

    @Override
    public void destroy() {

    }
}
